/*
 * Copyright (C) 2015-2017 Alibaba Group Holding Limited
 */

#ifndef _FLEXSPI_NORFLASH_
#define _FLEXSPI_NORFLASH_
 
#ifdef __cplusplus
 extern "C" {
#endif

#include <stddef.h>
#include "fsl_device_registers.h"
#include "fsl_common.h"

/*******************************************************************************
 * Definitions
 ******************************************************************************/
/*${macro:start}*/
#define FLASH_FLEXSPI FLEXSPI
#define FLASH_SIZE 0x2000 /* 32Mb/KByte */
#define FLASH_FLEXSPI_AMBA_BASE FlexSPI_AMBA_BASE
#define FLASH_PAGE_SIZE 256
#define SECTOR_SIZE 0x1000 /* 4K */
#define FLASH_FLEXSPI_CLOCK kCLOCK_FlexSpi
#define FLASH_START_ADDR    0x60000000

#define NOR_CMD_LUT_SEQ_IDX_READ_NORMAL 7
#define NOR_CMD_LUT_SEQ_IDX_READ_FAST 13
#define NOR_CMD_LUT_SEQ_IDX_READ_FAST_QUAD 0
#define NOR_CMD_LUT_SEQ_IDX_READSTATUS 1
#define NOR_CMD_LUT_SEQ_IDX_WRITEENABLE 2
#define NOR_CMD_LUT_SEQ_IDX_ERASESECTOR 3
#define NOR_CMD_LUT_SEQ_IDX_PAGEPROGRAM_SINGLE 6
#define NOR_CMD_LUT_SEQ_IDX_PAGEPROGRAM_QUAD 4
#define NOR_CMD_LUT_SEQ_IDX_READID 8
#define NOR_CMD_LUT_SEQ_IDX_WRITESTATUSREG 9
#define NOR_CMD_LUT_SEQ_IDX_ENTERQPI 10
#define NOR_CMD_LUT_SEQ_IDX_EXITQPI 11
#define NOR_CMD_LUT_SEQ_IDX_READSTATUSREG 12
#define NOR_CMD_LUT_SEQ_IDX_ERASECHIP 5

#define CUSTOM_LUT_LENGTH 60
#define FLASH_QUAD_ENABLE 0x40
#define FLASH_BUSY_STATUS_POL 1
#define FLASH_BUSY_STATUS_OFFSET 0


/*API*/
status_t flexspi_nor_enable_quad_mode(FLEXSPI_Type *base);

status_t flexspi_nor_get_vendor_id(FLEXSPI_Type *base, uint8_t *vendorId);

void flexspi_nor_flash_init(FLEXSPI_Type *base);

status_t flexspi_nor_flash_read(uint32_t address, void *data, uint32_t size);

status_t flexspi_nor_flash_write(uint32_t address, const void *data, uint32_t size);

status_t flexspi_nor_flash_erase(uint32_t address, uint32_t size);

#ifdef __cplusplus
}
#endif

#endif /* _FLEXSPI_NORFLASH_ */

